﻿Shader "Unlit/Fire"
{
    Properties
    {
        _Mask ("R:外焰 G:内焰 B:透贴", 2D) = "blue" {}
        _Noise ("R:噪通1 G:噪通2 ", 2D) = "gary" {}
        _NoiseParams1 ("噪声1 X:大小 Y:流速 Z:强度", vector) = (1.0, 0.2, 0.2, 1.0)
        _NoiseParams2 ("噪声2 X:大小 Y:流速 Z:强度", vector) = (1.0, 0.2, 0.2, 1.0)
        _ColorOut ("外焰颜色", color) = (1,1,1,1)
        _ColorIn ("内焰颜色", color) = (1,1,1,1)

    }
    SubShader
    {
        Tags { 
            "Queue" = "Transparent"                     // 调整渲染顺序
            "RenderType"="TransparentCutout"            // 改为 cutout
            "ForceNoShadowCasting" = "True"             // 关闭阴影投射
            "IgnoreProjector" = "True"                  // 不响应投射器
            }
        LOD 100

        Pass
        {
            Name "FORWARD"
            Tags {
                "LightMode"="ForwardBase"
            }

            Blend One OneMinusSrcAlpha
            
            
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"
            #pragma multi_compile_fwdbase_fullshadows
            #pragma target 3.0

            // 输入参数
            uniform sampler2D _Mask;
            uniform sampler2D _Noise;
            uniform half3 _NoiseParams1;
            uniform half3 _NoiseParams2;
            uniform half3 _ColorOut;
            uniform half3 _ColorIn;

            struct VertexInput {
                float4 vertex : POSITION;
                float2 uv0 : TEXCOORD0;
            };

            struct VertexOutput {
                float4 pos : SV_POSITION;
                float2 uv0 : TEXCOORD0;
                float2 uv1 : TEXCOORD1;
                float2 uv2 : TEXCOORD2;
            };

            VertexOutput vert (VertexInput v) {
                VertexOutput o = (VertexOutput)0;
                o.pos = UnityObjectToClipPos( v.vertex );
                o.uv0 = v.uv0;
                o.uv1 = v.uv0 * _NoiseParams1.x - float2(0.0, frac(_Time.x * _NoiseParams1.y));              // frac 取余
                o.uv2 = v.uv0 * _NoiseParams2.x - float2(0.0, frac(_Time.x * _NoiseParams2.y));              // frac 取余
                return o;
            }
            float4 frag(VertexOutput i) : COLOR {
                // 扰动遮罩
                half warpMask = tex2D(_Mask , i.uv0).b;

                // 噪声
                half var_Noise1 = tex2D(_Noise, i.uv1).r;
                half var_Noise2 = tex2D(_Noise, i.uv2).g;
                // 噪声混合
                half noise = var_Noise1 * _NoiseParams1.z + var_Noise2 * _NoiseParams2.z;
                // 扰动uv
                float2 warpUV = i.uv0 - float2(0.0, noise) * warpMask;
                // mask 采样
                half3 var_Mask = tex2D(_Mask, warpUV);

                half3 finalRGB = _ColorOut * var_Mask.r + _ColorIn * var_Mask.g;
                
                half opacity = var_Mask.r + var_Mask.g;


                return half4(finalRGB, opacity);
            }
            ENDCG
        }
    }
    FallBack "Diffuse"
    CustomEditor "ShaderForgeMaterialInspector"
}
